package com.zcareu.logServ.controller;

import com.zcareu.logServ.entity.Filter;
import com.zcareu.logServ.repo.FilterRepo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FilterController {
    @Autowired
    private FilterRepo filterRepo;

    @RequiresPermissions("filter:create")
    @PostMapping(value = "/filter")
    public Filter save(@RequestParam String name, @RequestParam List<String> queries) {

        Filter filter = new Filter();

        filter.setName(name);
        filter.setQueries(queries);

        return filterRepo.save(filter);
    }

    @GetMapping(value = "/filter")
    public Page<Filter> index(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return filterRepo.findAll(PageRequest.of(page, size));
    }
}
